package com.haizhi.crm.api.controller.auth;

import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.sys.auth.constant.AuthStatus;
import com.haizhi.crm.sys.auth.service.UserService;
import com.haizhi.crm.sys.shiro.model.LoginQo;
import com.haizhi.crm.sys.shiro.model.UserRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by caochao on 2018/05/22.
 */
@Api(description = "[权限]-登陆、退出、用户权限" )
@RestController
@RequestMapping("/auth" )
public class AuthController {
    private static final GLog LOG = LogFactory.getLogger(AuthController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(
            value = "用户登录",
            notes = "用户登录 输入参数 {\"password\": \"123456\",\"userNo\": \"admin\"}",
            consumes = MediaType.APPLICATION_JSON_VALUE
    )
    @PostMapping(value = "login" )
    public Response login(@RequestBody @Valid LoginQo loginQo) {
        String userNo = loginQo.getUserNo();
        String password = loginQo.getPassword();
        try {
            Subject currentUser = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(userNo, password);
            token.setRememberMe(true);
            currentUser.login(token);
            UserRoleVo user = (UserRoleVo) currentUser.getPrincipal();
            currentUser.getSession().setAttribute("userID", user.getId());
        } catch (Exception e) {
            LOG.error(e);
            return Response.error(AuthStatus.NAME_PASSWD_MISMATCH);
        }
        return Response.success();
    }

    @ApiOperation(value = "用户登出", notes = "用户登出" )
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public Response logout() {
        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.logout();
        } catch (Exception e) {
            LOG.error(e);
            return Response.error(AuthStatus.USER_LOGOUT_FAIL);
        }
        return Response.success();
    }

    @ApiOperation("未授权警告" )
    @RequestMapping(value = "unauth", method = RequestMethod.GET)
    public Response unauth() {
        return Response.error(AuthStatus.USER_UNAUTH);
    }

    @ApiOperation(value = "查询用户信息", notes = "查询用户信息" )
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Response info() {
        Response info = userService.detail();
        if (info != null) {
            return info;
        }
        return Response.error().setMessage(AuthStatus.USER_NOT_LOGINED);
    }

    @ApiOperation(value = "重新加载权限", notes = "重新加载权限" )
    @RequestMapping(value = "reloadPermissions", method = RequestMethod.GET)
    public Response reloadPermissions() {
        try {
            userService.reloadPermission();
        } catch (Exception e) {
            Response.error(AuthStatus.USER_RELOAD_PERMISSION_FAIL);
        }
        return Response.success();
    }

    @ApiOperation(value = "页面渲染-查询用户资源权限" )
    @GetMapping(value = "findPermissions" )
    public Response findPermissions(@RequestParam Long parentId, @RequestParam(required = false) String companyKey) {
        return userService.findPermissions(parentId, companyKey);
    }

    @ApiOperation(value = "确认密码-公用" )
    @GetMapping(value = "confirmPassword" )
    public Response confirmPassword(@RequestParam String password) {
        return userService.confirmPassword(password);
    }
}
